package com.yllt4cloud.push.util;

import com.yllt4cloud.common.core.constant.CacheConstants;
import com.yllt4cloud.common.core.constant.SecurityConstants;
import com.yllt4cloud.common.core.constant.SystemConfigConstants;
import com.yllt4cloud.common.core.util.ResultResp;
import com.yllt4cloud.common.ddbase.constant.NettyConstants;
import com.yllt4cloud.common.ddbase.message.Message;
import com.yllt4cloud.push.server.HandlerDispatcher;
import com.yllt4cloud.shangmi.api.entity.ShangTaiHuiZongXinXi;
import com.yllt4cloud.shangmi.api.feign.RemoteChuQinService;
import com.yllt4cloud.shangmi.api.feign.RemoteHuaDanService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: yaoxj
 * @create: 2019-12-19 17:58
 **/
@Component
@Slf4j
@AllArgsConstructor
public class SocketUtil {
    private final RemoteChuQinService remoteChuQinService;
    private final RemoteHuaDanService remoteHuaDanService;

    private RedisTemplate redisTemplate;
    private HandlerDispatcher handlerDispatcher;

    public void sendShangMiRoomInfo(Integer roomId) {
        QueryRoomListParam param = new QueryRoomListParam();
        param.setRoomId(roomId);
        List<QueryShangMiRoomListResult> list =  shangMiRoomService.queryRoomList(param);
        if (list.size() > 0) {
            QueryShangMiRoomListResult result = list.get(0);
            handlerDispatcher.sendGroupMessage(NettyConstants.SHAN_MI_ROOM_MSG_GROUP_NAME, Message.success(JsonUtil.parseMap(result)));
        }
    }

    public void sendShanMiKaoQinRenShu(){
        try {
            Map<String, Object> paramMap = new HashMap<>();
            redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(String.class));
            String  kaoQinRiQi= (String) redisTemplate.opsForHash().get(CacheConstants.SYSTEMCONFIG_KEY, SystemConfigConstants.SYSTEM_TIME);
            ResultResp countChuQin=remoteChuQinService.countChuQin(kaoQinRiQi,SecurityConstants.FROM_IN);
            ShangTaiHuiZongXinXi shangTaiHuiZongXinxi=remoteHuaDanService.shangTaiHuiZongXinxi(SecurityConstants.FROM_IN);
            paramMap.clear();
            paramMap.put("chuQinCount", countChuQin.getResult());
            paramMap.put("shangTaiLv", shangTaiHuiZongXinxi.getShangTaiLv());
            handlerDispatcher.sendGroupMessage(NettyConstants.SHAN_MI_KAO_QIN_COUNT_GROUP_NAME, Message.success(paramMap));
        } catch (Exception e) {
            log.error("推送异常：{}",e.getMessage());
        }
    }

}
